package com.meihuayishu.backend.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Builder;

import jakarta.persistence.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 占卜记录实体
 * 对应数据库中的 divination_records 表
 */
@Entity
@Table(name = "divination_records")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DivinationRecord {
    
    @Id
    @Column(name = "record_id", length = 50)
    private String recordId;
    
    @Column(name = "user_id", length = 50, nullable = false)
    private String userId;
    
    @Column(name = "hexagram_name", length = 50, nullable = false)
    private String hexagramName;
    
    @Column(name = "hexagram_code", length = 50)
    private String hexagramCode;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "divination_type", nullable = false)
    private DivinationType divinationType;
    
    @Column(name = "question", columnDefinition = "TEXT")
    private String question;
    
    @Column(name = "algorithm", length = 50)
    private String algorithm;
    
    @Column(name = "input_data", columnDefinition = "JSON")
    private String inputDataJson;
    
    @Column(name = "hexagram_data", columnDefinition = "JSON", nullable = false)
    private String hexagramDataJson;
    
    @Column(name = "ai_interpretation", columnDefinition = "TEXT")
    private String aiInterpretation;
    
    @Column(name = "ai_interpretation_id", length = 50)
    private String aiInterpretationId;
    
    @Column(name = "feedback_rating")
    private Integer feedbackRating;
    
    @Column(name = "feedback_comment", columnDefinition = "TEXT")
    private String feedbackComment;
    
    @Column(name = "is_favorite", nullable = false)
    private Boolean isFavorite = false;
    
    @Column(name = "is_private", nullable = false)
    private Boolean isPrivate = false;
    
    @Column(name = "tags", columnDefinition = "JSON")
    private String tagsJson;
    
    @Column(name = "share_count", nullable = false)
    private Integer shareCount = 0;
    
    @Column(name = "view_count", nullable = false)
    private Integer viewCount = 0;
    
    @Column(name = "experience_gained")
    private Integer experienceGained;
    
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;
    
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    @Column(name = "favorited_at")
    private LocalDateTime favoritedAt;
    
    @PrePersist
    protected void onCreate() {
        if (createdAt == null) {
            createdAt = LocalDateTime.now();
        }
        if (updatedAt == null) {
            updatedAt = LocalDateTime.now();
        }
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
    
    /**
     * 占卜类型枚举
     */
    public enum DivinationType {
        TIME("time"),
        NUMBER("number"), 
        IMAGE("photo"),
        TEXT("voice");
        
        private final String value;
        
        DivinationType(String value) {
            this.value = value;
        }
        
        public String getValue() {
            return value;
        }
        
        public static DivinationType fromValue(String value) {
            for (DivinationType type : DivinationType.values()) {
                if (type.value.equals(value) || type.name().equals(value)) {
                    return type;
                }
            }
            return TEXT; // 默认值
        }
    }
}